"""
安装脚本
"""
import os
import sys
import subprocess
from pathlib import Path

def install_requirements():
    """安装依赖包"""
    print("正在安装依赖包...")

    # 安装基础依赖
    requirements = [
        "fastapi==0.104.1",
        "uvicorn[standard]==0.24.0",
        "python-multipart==0.0.6",
        "jinja2==3.1.2",
        "pymilvus==2.4.3",
        "mysql-connector-python==8.2.0",
        "sqlalchemy==2.0.23",
        "sentence-transformers==2.2.2",
        "transformers==4.35.2",
        "torch==2.1.1",
        "numpy==1.24.3",
        "pypdf2==3.0.1",
        "pdfplumber==0.10.3",
        "python-docx==1.1.0",
        "openpyxl==3.1.2",
        "python-pptx==0.6.23",
        "pandas==2.1.3",
        "pillow==10.1.0",
        "pytesseract==0.3.10",
        "tqdm==4.66.1",
        "python-dotenv==1.0.0",
        "pydantic==2.5.0",
        "aiofiles==23.2.1"
    ]

    for package in requirements:
        try:
            print(f"安装 {package}...")
            subprocess.check_call([sys.executable, "-m", "pip", "install", package])
        except subprocess.CalledProcessError as e:
            print(f"安装 {package} 失败: {e}")
            return False

    print("依赖包安装完成")
    return True

def create_directories():
    """创建必要的目录"""
    print("创建目录结构...")

    directories = [
        "data/files",
        "data/models",
        "data/temp",
        "logs",
        "vector_system/web/static",
        "vector_system/web/templates"
    ]

    for directory in directories:
        Path(directory).mkdir(parents=True, exist_ok=True)
        print(f"创建目录: {directory}")

    print("目录创建完成")

def create_env_file():
    """创建环境配置文件"""
    print("创建环境配置文件...")

    env_content = """        # 数据库配置
        MYSQL_HOST=117.72.102.95
        MYSQL_PORT=16033
        MYSQL_USER=test
        MYSQL_PASSWORD=StrongPassword123!
        MYSQL_DATABASE=vector_system

# Milvus配置
MILVUS_HOST=192.168.224.255
MILVUS_PORT=19530
MILVUS_USER=
MILVUS_PASSWORD=
MILVUS_DB=default

# 向量模型配置
VECTOR_MODEL=all-mpnet-base-v2
VECTOR_MODEL_PATH=data/models/sentence-transformers
DEVICE=cpu

# Rerank模型配置
RERANK_MODEL=BAAI/bge-reranker-v2-m3
RERANK_MODEL_PATH=data/models/rerank

# Web服务配置
HOST=0.0.0.0
PORT=8000
DEBUG=False
RELOAD=True
WORKERS=1

# 日志配置
LOG_LEVEL=INFO

# 缓存配置
ENABLE_CACHE=True
REDIS_HOST=117.72.102.95
REDIS_PORT=19736
REDIS_PASSWORD=Aresenyang@1217
REDIS_DB=2
"""

    with open(".env", "w", encoding="utf-8") as f:
        f.write(env_content)

    print("环境配置文件创建完成: .env")

def main():
    """主安装函数"""
    print("向量库操作系统安装程序")
    print("=" * 50)

    # 1. 安装依赖
    if not install_requirements():
        print("依赖安装失败，请检查网络连接和Python环境")
        return

    # 2. 创建目录
    create_directories()

    # 3. 创建环境配置
    create_env_file()

    print("\n安装完成！")
    print("\n下一步：")
    print("1. 配置 .env 文件中的数据库和Milvus连接信息")
    print("2. 启动MySQL数据库")
    print("3. 启动Milvus服务")
    print("4. 运行 python vector_system/main.py 启动系统")
    print("\n访问地址：")
    print("- Web界面: http://localhost:8000")
    print("- API文档: http://localhost:8000/docs")

if __name__ == "__main__":
    main()
